﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Data;

namespace PickGold.Base
{
	/// <summary>
	/// 权限授权点。
	/// </summary>
	[ComVisible(true)]
	public interface IGrant
	{
		/// <summary>
		/// 授权键。
		/// </summary>
		string Key { set; get; }

		/// <summary>
		/// 授权数据。
		/// </summary>
		string Data { set; get; }

		/// <summary>
		/// 授权值。
		/// </summary>
		decimal Value { set; get; }

		/// <summary>
		/// 默认授权索引；在权限测试接口实现中无需赋值此属性，而是返回新值。
		/// </summary>
		int AllowIndex { set; get; }

		/// <summary>
		/// 默认拒绝索引。
		/// </summary>
		int DenyIndex { set; get; }
	}

	/// <summary>
	/// 权限授权点。
	/// </summary>
	[ComVisible(true)]
	public interface IGrantPoint : IGrant
	{
		/// <summary>
		/// 权限检测器，而页可继承此接口自动实现绑定。
		/// </summary>
		IGrantTester Tester { set; get; }

		/// <summary>
		/// 授权组。
		/// </summary>
		string Group { set; get; }

		/// <summary>
		/// 开始检测事件。
		/// </summary>
		event GrantEventHandler BeginGrantTest;

		/// <summary>
		/// 结束检测事件。
		/// </summary>
		event GrantEventHandler AfterGrantTest;
	}

	/// <summary>
	/// 权限授权访问接口。
	/// </summary>
	public interface IGrantTester
	{
		/// <summary>
		/// 取授权索引。
		/// </summary>
		/// <param name="grant">权限点控件。</param>
		/// <returns></returns>
		int GetGrantAllow(IGrant grant);
	}

	/// <summary/>
	[Serializable]
	[ComVisible(true)]
	[ClassInterface(ClassInterfaceType.None)]
	[AttributeUsage(AttributeTargets.All, Inherited = true, AllowMultiple = true)]
	public class GrantAttribute : Attribute, IGrant
	{
		private ICustomAttributeProvider _Provider;
		private object[] _Parameters;

		/// <summary/>
		public GrantAttribute()
			: base()
		{
			this._Parameters = null;
		}

		/// <summary/>
		public GrantAttribute(object[] parageters)
			: base()
		{
			this._Parameters = parageters;
		}

		/// <summary>
		/// 授权键。
		/// </summary>
		public string Key { set; get; }

		/// <summary>
		/// 授权数据。
		/// </summary>
		public string Data { set; get; }

		/// <summary>
		/// 授权值。
		/// </summary>
		public decimal Value { set; get; }

		/// <summary>
		/// 默认授权索引；在权限测试接口实现中无需赋值此属性，而是返回新值。
		/// </summary>
		public int AllowIndex { set; get; }

		/// <summary>
		/// 默认拒绝索引。
		/// </summary>
		public int DenyIndex { set; get; }

		/// <summary>
		/// 属性提供者
		/// </summary>
		public ICustomAttributeProvider Provider
		{
			set
			{
				this._Provider = value;
			}
			get
			{
				return this._Provider;
			}
		}

		/// <summary>
		/// Ajax 调用参数
		/// </summary>
		public object[] Parameters
		{
			set
			{
				this._Parameters = value;
			}
			get
			{
				return this._Parameters;
			}
		}
	}

	/// <summary/>
	[Serializable]
	[ComVisible(true)]
	[ClassInterface(ClassInterfaceType.None)]
	public class GrantEventArgs : EventArgs
	{
		private IGrant _Grant;
		private int _Result;

		/// <summary/>
		public GrantEventArgs(IGrant grant)
		{
			this._Grant = grant;
			this._Result = grant.AllowIndex;
		}

		/// <summary/>
		public GrantEventArgs(IGrant grant, int result)
		{
			this._Grant = grant;
			this._Result = result;
		}

		/// <summary/>
		public IGrant Grant
		{
			get
			{
				return this._Grant;
			}
		}

		/// <summary/>
		public int Result
		{
			set
			{
				this._Result = value;
			}
			get
			{
				return this._Result;
			}
		}
	}

	public delegate void GrantEventHandler(object sender, GrantEventArgs e);
}
